package com.zirenx.sys.controller;

import com.zirenx.annotation.RouteMapping;
import com.zirenx.common.controller.BaseController;
import com.zirenx.common.controller.IController;
import com.zirenx.common.redis.RedisManager;
import com.zirenx.common.redis.RedisModel;
import com.zirenx.utils.LogUtils;
import com.zirenx.utils.R;
import redis.clients.jedis.exceptions.JedisConnectionException;

import java.net.ConnectException;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 缓存管理
 */
@RouteMapping("/sys/cache")
public class CacheController extends BaseController implements IController {

    RedisManager redisManager = new RedisManager(true);

    @Override
    public void index() {

    }

    @Override
    public void dataAjax() {
        R r = R.ok();
        try {
            List<RedisModel> all = redisManager.all();
            r.put("rows", all);
            r.put("total",all.size());
        }catch (Exception e){
            LogUtils.error(this.getClass(),e);
            r = R.error();
        }
        renderJson(r);
    }

    @Override
    public void add() {

    }

    @Override
    public void save() {
        try {
            String key = get("key");
            String value = get("value");
            redisManager.set(key,value);
            renderJson(R.ok());
        }catch (JedisConnectionException e){
            LogUtils.error(this.getClass(),e);
            renderJson(R.error().put("msg","连接失败"));
        }catch (Exception e){
            LogUtils.error(this.getClass(),e);
            renderJson(R.error());
        }
    }

    @Override
    public void edit() {

    }

    @Override
    public void update() {
        String key = get("key");
        String value = get("value");
        redisManager.set(key,value);
        renderJson(R.ok());
    }

    @Override
    public void remove() {

    }

    @Override
    public void batchRemove() {

    }
}
